<template>
  <div
    class="editable-element-video"
    :class="{ lock: elementInfo.lock }"
    :style="{
      top: elementInfo.top + 'px',
      left: elementInfo.left + 'px',
      width: elementInfo.width + 'px',
      height: elementInfo.height + 'px',
    }"
  >
    <div
      class="rotate-wrapper"
      :style="{ transform: `rotate(${elementInfo.rotate}deg)` }"
    >
      <div
        class="element-content"
        v-contextmenu="contextmenus"
        @mousedown="($event) => handleSelectElement($event, false)"
        @touchstart="($event) => handleSelectElement($event, false)"
      >
        <div>
          <video
            :style="{
              width: elementInfo.width + 'px',
              height: elementInfo.height + 'px',
              opacity: elementInfo.opacity,
              'object-fit': 'fill'
            }"
            ref="player"
            @ended="handleVideoEnded"
            :src="elementInfo.urls[index]"
          ></video>
        </div>

        <div
          :class="['handler-border', item]"
          v-for="item in ['t', 'b', 'l', 'r']"
          :key="item"
          @mousedown="($event) => handleSelectElement($event)"
          @touchstart="($event) => handleSelectElement($event)"
        ></div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { PropType, ref, computed, watch } from "vue";
import { storeToRefs } from "pinia";
import { useMainStore } from "@/store";
import { PPTVideoElement } from "@/types/slides";
import { ContextmenuItem } from "@/components/Contextmenu/types";

const props = defineProps({
  elementInfo: {
    type: Object as PropType<PPTVideoElement>,
    required: true,
  },
  selectElement: {
    type: Function as PropType<
      (
        e: MouseEvent | TouchEvent,
        element: PPTVideoElement,
        canMove?: boolean
      ) => void
    >,
    required: true,
  },
  contextmenus: {
    type: Function as PropType<() => ContextmenuItem[] | null>,
  },
});
const player = ref<null>(null);
const index = ref(0);
const currentVideo = computed(() => props.elementInfo.urls[index.value]);
function handleVideoEnded() {
  if (index.value < props.elementInfo.urls.length - 1) {
    index.value++;
    player.value.urls = currentVideo.value;
    player.value.play();
  }
}

watch(index, (newValue) => {
  player.value.urls = currentVideo.value;
  player.value.play();
});

const { canvasScale } = storeToRefs(useMainStore());

const handleSelectElement = (e: MouseEvent | TouchEvent, canMove = true) => {
  if (props.elementInfo.lock) return;
  e.stopPropagation();

  props.selectElement(e, props.elementInfo, canMove);
};
</script>

<style lang="scss" scoped>
.editable-element-video {
  position: absolute;

  &.lock .handler-border {
    cursor: default;
  }
}
.rotate-wrapper {
  width: 100%;
  height: 100%;
}
.element-content {
  width: 100%;
  height: 100%;
  position: relative;
}
.handler-border {
  position: absolute;
  cursor: move;

  &.t {
    width: 100%;
    height: 20px;
    top: 0;
    left: 0;
  }
  &.b {
    width: 100%;
    height: 5px;
    bottom: 0;
    left: 0;
  }
  &.l {
    width: 10px;
    height: 100%;
    left: 0;
    top: 0;
  }
  &.r {
    width: 10px;
    height: 100%;
    right: 0;
    top: 0;
  }
}
</style>
